<template>
  <div>
    <el-card class="elcard" style="margin-bottom:20px;">
      <!-- 搜索 -->
      <el-autocomplete
        v-model.trim="searchText"
        :autofocus="false"
        :fetch-suggestions="querySearchAsync"
        clearable
        placeholder="请输入高校或机构名称"
        size="medium"
        class="header-search-bar"
        @select="init(1)"
        @keyup.enter.native="init(1)">
        <i slot="suffix" class="el-input__icon el-icon-search search-btn" @click="init(1)"></i>
      </el-autocomplete>
      <el-button class ="button" type="primary" @click="init(1)">查询</el-button>
      <dl class="screen-box">
        <dd>
          <span class="tit">地区</span>
          <el-select v-model="regionValue" @change="init(1)" size="small" placeholder="全部地区" class="elselect">
            <el-option
              v-for="item in regionOptions"
              :key="item.criCode"
              :label="item.criName"
              :value="item.criName">
            </el-option>
          </el-select>  
        </dd>
        <dd>
          <span class="tit">机构类型</span>
          <el-select v-model="patentsNumValue" @change="init(1)" size="small" placeholder="全部" class="elselect">
            <el-option
              v-for="item in patentsNumOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>  
        </dd>
      </dl>
    </el-card>
    <!--  -->
    <el-card :body-style="{'padding':'0'}" class="elcard">
      <ul v-if="list.length>0" class="list-box" v-loading="loading">
        <li v-for="(item, index) in list" :key="index">
          <div class="logo-box">
            <img width="100%" height="100%" v-if="item.logo_url !== ''" :src="item.logo_url" alt="">
            <img width="100%" height="100%" v-else src="../../../../assets/img/default.png" alt="">
          </div>
          <h2 class="name" @click="goDetail(item)">{{item.name}}</h2>
          <p class="info">
            <!-- <span>法人代表：<i>{{item.doc_count}}</i></span> -->
            <span>地址：<i>{{item.address}}</i></span>
            <!-- <span>邮箱：<i>{{item.phone}}</i></span> -->
          </p>
          <p class="info">
            <span>
              <em>业务范围：</em>
              <span>{{item.scope}}</span>
              <!-- <el-rate
                v-model="item.star"
                disabled
                disabled-void-icon-class="el-icon-star-off"
                class="elrate">
              </el-rate> -->
            </span>
          </p>
        </li>
      </ul>
      <ul v-if="list===''" class="no-data">
        <li>暂无数据</li>
      </ul>
      <el-col :span="24" style="text-align:center;padding:10px; 0">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :current-page.sync="ipi"
          :page-size="ips"
          :total="total">
        </el-pagination>
      </el-col>
    </el-card>
  </div>
</template>

<script>
import mock from './mockSearch.json'
import axios from 'axios'
let types = null
import {mapState} from 'vuex'
export default {
  data() {
    return {
      searchText: '',
      querySearchList: [],
      cancel2: null,
      regionValue: '不限',
      regionOptions: [],
      fieldValue: '',
      fieldOptions: [],
      patentsNumValue: '不限',
      patentsNumOptions: [
        {name:'不限',value:'不限'},
        {name:'高校',value:'高校'},
        {name:'机构',value:'院所'}
      ],
      papersNumValue: '',
      papersNumOptions: [],
      // 日期选择
      startYear: '',
      endYear: '',
      pickerStart: {
        disabledDate: (time) => {
          if(this.endYear&&this.endYear!==''){
            if(time.getFullYear()>Number(this.endYear)){
              return true
            }
            return false
          }
        }
      },
      pickerEnd: {
        disabledDate: (time) => {
          if(this.startYear&&this.startYear!==''){
            if(time.getFullYear()<Number(this.startYear)){
              return true
            }
            return false
          }
        }
      },
      listData: [],
      list: [],
      ips: 5,
      total: 0,
      ipi: 1,
      loading:false
    }
  },
  computed:{
    ...mapState(['levelCode', 'levelName'])
  },
  created(){
    // 获取所有省级单位
    this.$post(this.$Url.patent.regionInfo,
      {
        "supId":"000000" // 根据上级ID查询所属下级机构，最顶级criCode是 000000  苏州320000
      }
    ).then((res)=>{
      if(res){
        res.data.forEach(i=>{
          if(i.criName.length==2){
            i.criName = i.criName+'市'
          }
        })

        let all = {
          criCode: '',
          criName: '不限'
        }

        this.regionOptions = [all, ...res.data]
      }
    }).catch(e=>{
      console.log(e)
    })
  },
  mounted() {
    this.init(1, true)
  },
  methods: {
    // 跳到详情
    goDetail(val){
      let Jsons = {
        name:val.name,
        eid : val.eid,
        type: 'org'
      }
      this.$Bus.$emit("progressBar", Jsons)
    },
    init(ipi, isFirst) {
      if (ipi) {
        this.ipi = ipi
      }
      let Jsons = {
        musts: {}
      }
      this.loading = true

      if (isFirst) {
        Jsons['musts']['province'] = this.levelName
      } else {
        let patentsNumValue = this.patentsNumValue !== '不限' ? this.patentsNumValue : null
        let regionValue = this.regionValue !== '不限' ? this.regionValue : null

        if (this.searchText) {
          Jsons['matchPhrase'] = {
            name: this.searchText
          }
        }

        if (regionValue || patentsNumValue) {
          if (regionValue) {
            Jsons['musts']['province'] = regionValue
          }
          if (patentsNumValue) {
            Jsons['musts']['type.keyword'] = patentsNumValue
          }
        }
      }

      

      this.$post(this.$Url.organization.research,{
        "ipi": this.ipi,
        "ips": this.ips,
        ...Jsons
      }).then(res=>{
        this.loading = false
        if(res.data.result.length && res.data.result.length>0){
          this.list = res.data.result
          this.total = res.data.page.totalRow
        }else{
          this.list = ''
          this.total = 0
        }
      }).catch(e=>{
        console.log(e)
      })
    },
    // 自动补全
    querySearchAsync(queryString, cb) {
      if(this.cancel2!==null){
        this.cancel2()
      }
      if(this.searchText===''){
        cb([])
        return
      }
      let ths = this
      this.querySearchList = []
      this.$get(this.$Url.production.searchGaoxiaoJigou, {
        name: queryString || this.searchText
      }, new axios.CancelToken(function executor(c) {
        ths.cancel2 = c
      })).then((res) => {
        let list = res.data ? res.data : [];
        list.forEach(v=>{
          this.querySearchList.push({'value': v.name})
        });
        cb(this.querySearchList)
      })
      
    },
    // 分页
    handleCurrentChange(val){
      this.ipi = val
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>
.elcard {
  text-align: left;
  .header-search-bar {
    position: relative;
    width: 40%;
    margin-left: 0;
    margin-right: 20px;
  }
  .button {
    width: 100px;
  }
  .screen-box {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    margin-top: 30px;
    dd {
      width: 33.33%;
      margin-bottom: 10px;
      .tit {
        display: inline-block;
        width: 56px;
        text-align: left;
        font-size: 12px;
        color: #4f515b;
      }
      .elselect {
        width: 200px;
        background-color: #f3f5f8;
      }
      .eldatepicker {
        width: 120px;
      }
      .split {
        display: inline-block;
        padding: 0 5px;
      }
    }
  }
}

.list-box {
  position: relative;
  list-style: none;
  li {
    position: relative;
    display: block;
    min-height: 140px;
    padding-top: 20px;
    padding-bottom: 20px;
    padding-left: 140px;
    border-bottom: 1px solid #e5e7f0;
    .logo-box {
      position: absolute;
      top: 20px;
      left: 20px;
      width: 100px;
      height: 100px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      border: 1px solid #d9e0ee;
      box-sizing: border-box;
    }
    h2 {
      width: 100%;
      margin-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: #4f515b;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .info {
      display: flex;
      align-items: center;
      width: 80%;
      margin-top: 10px;
      span {
        flex: 1;
        font-size: 12px;
        color: #86899e;
        i {
          font-style: normal;
          color: #4f515b;
        }
        em {
          vertical-align: middle;
        }
      }
      .elrate {
        display: inline-block;
        vertical-align: unset;
      }
    }
  }
}
.no-data {
  list-style: none;
  li {
    display: block;
    padding: 20px;
  }
}
</style>